package com.cheer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.dao.ReferenceReviewLogDao;
import com.cheer.dto.StandingBookReviewLogDTO;
import com.cheer.entity.Applicant;
import com.cheer.entity.ReferenceEntity;
import com.cheer.entity.ReferenceReviewLogEntity;
import com.cheer.enums.RegisterStatusEnum;
import com.cheer.enums.RoleEnum;
import com.cheer.enums.TrueOrFalseEnum;
import com.cheer.service.ReferenceReviewLogService;
import com.cheer.service.ReferenceService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service("referenceReviewLogService")
public class ReferenceReviewLogServiceImpl extends ServiceImpl<ReferenceReviewLogDao, ReferenceReviewLogEntity> implements ReferenceReviewLogService {

    @Autowired
    private ReferenceReviewLogDao referenceReviewLogDao;

    @Autowired
    private ReferenceService referenceService;

    @Override
    public List<StandingBookReviewLogDTO> listByAccountId(Long applicantId, Integer type, Long referenceId) {

       return referenceReviewLogDao.listByAccountId(applicantId, type, referenceId);
    }

    @Override
    public List<StandingBookReviewLogDTO> listByAccountId(Applicant applicant, Integer type) {

        Long applicantId = applicant.getId();
        String vintage = applicant.getVintage();

        List<StandingBookReviewLogDTO> result = new ArrayList<>();
        ReferenceEntity referenceEntity = referenceService.getByAppIdAndVintage(applicantId, vintage);

        if(referenceEntity != null){
            result = listByAccountId(applicantId, type, referenceEntity.getId());
        }

        return result;
    }

    @Override
    public List<Long> getSubmitAgainIds(Integer userRole) {
        // 查询所有状态为正在参评的信息
        List<ReferenceEntity>  lists = new ArrayList<>();
        if(userRole.equals(RoleEnum.SECOND_ADMIN.getCode())
                || userRole.equals(RoleEnum.LOWER_ADMIN.getCode())){
            lists = referenceService.selectByRegisterStatus(RegisterStatusEnum.PROVINCE_REVIEW);
        }
        if(userRole.equals(RoleEnum.SUPER_ADMIN.getCode())){
            Integer isPost = TrueOrFalseEnum.TRUE.getType();
            lists = referenceService.selectByRegisterStatusAndPost(RegisterStatusEnum.PROVINCE_REVIEW, isPost);
        }
        List<Long> referenceIds = lists.stream().map(ReferenceEntity::getId).collect(Collectors.toList());
        List<Long> noPassRecord = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(referenceIds)){
            List<ReferenceReviewLogEntity>  noPassLogs =  referenceReviewLogDao.selectNoPassAndReferenceIds(referenceIds);
            noPassRecord = noPassLogs.stream().map(ReferenceReviewLogEntity::getReferenceId).distinct().collect(Collectors.toList());
        }

        return noPassRecord;
    }
}